package dao.daoimpl;

import util.DataBase;
import dao.AdministratorDao;
import dao.CommentDao;
import dao.LabelDao;
import bean.Article;
import bean.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class AdministratorImpl implements AdministratorDao {
    @Override
    public ArrayList<User> getAllUsers() throws SQLException {
        ArrayList<User> users=new ArrayList<>();
        String sql="select * from user_table limit 25";
        DataBase.init();
        ResultSet resultSet= DataBase.inquire(sql);
        while (resultSet.next()){
            User user=new User();
            user.setAccount(resultSet.getString("user_account"));
            user.setPsw(resultSet.getString("user_password"));
            user.setAddress(resultSet.getString("user_address"));
            user.setBirthday(resultSet.getString("user_birthday"));
            user.setAge(resultSet.getInt("user_age"));
            user.setName(resultSet.getString("user_name"));
            user.setScreenName(resultSet.getString("user_screenName"));
            user.setEmail(resultSet.getString("user_email"));
            user.setHeadPortrait(resultSet.getString("user_headPortrait"));
            user.setSign(resultSet.getString("user_sign"));
            user.setSex(resultSet.getString("user_sex"));
            user.setPhone(resultSet.getString("user_phone"));
            user.setIsAdmin(resultSet.getInt("user_isAdmin"));

            users.add(user);
        }
        DataBase.release();
        return users;
    }

    @Override
    public ArrayList<Article> getAllArticles() throws SQLException {
        String sql="select * from article_table limit 25";
        DataBase.init();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        ArrayList<Article> articles=new ArrayList<>();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            int id=resultSet.getInt("article_id");
            String content=resultSet.getString("article_content");
            int likingDegree=resultSet.getInt("article_likingDegree");
            int views=resultSet.getInt("article_views");
            String title=resultSet.getString("article_title");
            String time = resultSet.getString("article_time");
            int commentCount=resultSet.getInt("article_commentCount");
            int category=resultSet.getInt("article_categoryId");
            String userAccount=resultSet.getString("article_user");
            String type=resultSet.getString("article_type");
            String subfield=resultSet.getString("article_subfield");
            int approve=resultSet.getInt("article_approve");
            int like=resultSet.getInt("article_like");
            articles.add(new Article(id,content,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like));
        }
        DataBase.release();
        System.out.println(articles.size());
        return articles;
    }

    @Override
    public ArrayList<Article> getAllCommand() throws SQLException {
        ArrayList<Article> articles=new ArrayList<>();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        String sql="select article_table.* from article_table,command_table where article_table.article_id=command_table.command_articleId";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            int id=resultSet.getInt("article_table.article_id");
            String content=resultSet.getString("article_table.article_content");
            int likingDegree=resultSet.getInt("article_table.article_likingDegree");
            int views=resultSet.getInt("article_table.article_views");
            String title=resultSet.getString("article_table.article_title");
            String time = resultSet.getString("article_table.article_time");
            int commentCount=resultSet.getInt("article_table.article_commentCount");
            int category=resultSet.getInt("article_table.article_categoryId");
            String userAccount=resultSet.getString("article_table.article_user");
            String type=resultSet.getString("article_table.article_type");
            String subfield=resultSet.getString("article_table.article_subfield");
            int approve=resultSet.getInt("article_table.article_approve");
            int like=resultSet.getInt("article_table.article_like");
            articles.add(new Article(id,content,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like));
        }
        DataBase.release();
        return articles;
    }

    @Override
    public boolean deleteBlog(int articleId) {
        String sql="delete from article_table where article_id='"+articleId+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateBlog(Article article) {
        String time=article.getTime();
        int id=article.getId();
        String user=article.getUser();
        int category=article.getCategory();
        String title=article.getTitle();
        String type=article.getType();
        int commentCount=article.getCommentCount();
        int likingDegree=article.getLikingDegree();
        int views=article.getViews();
        String subfield=article.getSubfield();
        int like=article.getLike();
        int approve=article.getApprove();
        String sql="update article_table set article_time='"+time+"',article_categoryId='"+category+"',article_title='"+ title+"'," +
                "article_type='"+type+"',article_commentCount='"+commentCount+"',article_likingDegree='"+likingDegree
                +"',article_views='"+views+"',article_user='"+user+"',article_subfield='"+subfield+"',article_like='"+like+"',article_approve='"+approve+"' where article_id='"+id+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean commandBlog(int articleId) {
        String sql="insert into command_table(command_articleId) values('"+articleId+"')";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteCommand(int articleId) {
        String sql="delete from command_table where command_articleId='"+articleId+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUser(String account) {
        String sql="delete from user_table where user_account='"+account+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateUser(User user) {
        String sql="update user_table set user_email='"+user.getEmail()+"',user_account='"+user.getAccount()+"',user_password='"+user.getPsw()+"',user_screenName= '"+user.getScreenName()+"'" +
                ",user_sex='"+user.getSex()+"',user_sign='"+user.getSign()+"',user_age='"+user.getAge()+"',user_birthday='"+user.getBirthday()+"'," +
                "user_phone='"+user.getPhone()+"',user_address='"+user.getAddress()+"',user_name='"+user.getName()+"'" +
                "where user_account='"+user.getAccount()+"' and user_email='"+user.getEmail()+"'";
        DataBase.init();
        int flag= DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean checkBlog(int articleId) {
        String sql="update article_table set article_approve = 1 where article_id = '"+articleId+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }
}
